﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using App.Utilities.Web.Handlers;
using CRMBiz;
using System.Data;
using System.Text;
using CRMEntity;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for ApplicantCreditCheck
    /// </summary>
    public class ApplicantCreditCheck : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();
            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        public void GetClientDetalis(string app_id)
        {
            try
            {
                DataSet ds = ApplicantCreditCheckHandler.GetClientDetalis(app_id);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void GetSingleClientDetalis(string ContactID, string app_id)
        {
            try
            {
                DataSet ds = ApplicantCreditCheckHandler.GetSingleClientDetalis(ContactID, app_id);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void GetSingleVedaCreditCheckHistory(string ContactID)
        {
            try
            {
                DataSet ds = ApplicantCreditCheckHandler.GetSingleVedaCreditCheckHistory(ContactID);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void VedaCreditCheck(string app_id, string ContactID, string FirstGivenName, string MiddleName, string FamilyName, string Gender, string DriversLicenceNumber, string DateofBirth, string StreetNumber, string StreetName, string Suburb, string State, string PostCode, string LoanAmount, string ClientReference, string CreatedDate, string CreatedBy, string VedaFileNo, int flagByCreditCheck, string Company)
        {
            try
            {
                string XMLFile = ApplicantCreditCheckHandler.VedaCreditCheck(app_id, ContactID, FirstGivenName, MiddleName, FamilyName, Gender, DriversLicenceNumber, DateofBirth, StreetNumber, StreetName, Suburb, State, PostCode, LoanAmount, ClientReference, CreatedDate, CreatedBy, VedaFileNo, flagByCreditCheck, Company);
                context.Response.Write(XMLFile);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
                context.Response.Write(ex.Message);
            }
        }

        public void UpdateClientProfile(string app_id, string ContactID, string FirstGivenName, string MiddleName, string FamilyName, string Gender, string DriversLicenceNumber, string DateofBirth, string StreetNumber, string StreetName, string Suburb, string State, string PostCode)
        {
            try
            {
                ApplicantCreditCheckHandler.UpdateClientProfile(app_id, ContactID, FirstGivenName, MiddleName, FamilyName, Gender, DriversLicenceNumber, DateofBirth, StreetNumber, StreetName, Suburb, State, PostCode);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void CheckCurrentDate(string ContactID)
        {
            try
            {
                int flag = ApplicantCreditCheckHandler.CheckCurrentDate(ContactID);
                context.Response.Write(flag);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void PrepareDataGrid(string app_id, string CreatedDate, string FullName, string VedafileNo, string RecordAge, string Createby)
        {
            try
            {
                DataTable dtApplicant = ApplicantCreditCheckHandler.GetClientDetalis(app_id).Tables[0];
                StringBuilder datagrid = new StringBuilder();
                int j = 0;
                for (int i = 0; i < dtApplicant.Rows.Count; i++)
                {
                    DataTable dtCreditCheckHistory = ApplicantCreditCheckHandler.GetSingleVedaCreditCheckHistory(dtApplicant.Rows[i]["ContactID"].ToString()).Tables[0];

                    datagrid.Append("</br>");
                    datagrid.Append("<div style=\"float: left; width: 677px;\">");
                    datagrid.AppendFormat("<b>Applicant {0}: {1}</b>", i + 1, dtApplicant.Rows[i]["ClientName"].ToString());
                    datagrid.Append("</div>");
                    datagrid.Append("<div style=\"float: left; width: auto;\">");
                    if (dtCreditCheckHistory.Rows.Count > 0)
                        datagrid.AppendFormat("<a onclick=\"OpenVedaCheckDialog({0},{1})\" iconcls=\"icon-ok\" class=\"easyui-linkbutton l-btn\" href=\"javascript:void(0)\" group=\"\" id=\"\">", dtApplicant.Rows[i]["ContactID"].ToString(), dtCreditCheckHistory.Rows[0]["VedafileNO"].ToString());
                    else
                        datagrid.AppendFormat("<a onclick=\"OpenVedaCheckDialog({0},'')\" iconcls=\"icon-ok\" class=\"easyui-linkbutton l-btn\" href=\"javascript:void(0)\" group=\"\" id=\"\">", dtApplicant.Rows[i]["ContactID"].ToString());
                    datagrid.Append("<span class=\"l-btn-left\">");
                    datagrid.Append("<span class=\"l-btn-text icon-ok l-btn-icon-left\">Commence</span>");
                    datagrid.Append("</span>");
                    datagrid.Append("</a>");
                    datagrid.Append("</div>");
                    datagrid.Append("</br>");
                    datagrid.Append("<div class=\"panel datagrid\" style=\"width: 800px;\">");
                    datagrid.Append("<div class=\"datagrid-wrap panel-body panel-body-noheader\" title=\"\" style=\"width: 798px; height: auto;\">");
                    datagrid.Append("<div class=\"datagrid-view\" style=\"width: 798px; height: 127px;\">");
                    datagrid.Append("<div class=\"datagrid-view2\" style=\"width: 798px;\">");
                    datagrid.Append("<div class=\"datagrid-header\" style=\"width: 798px; height: 24px;\">");
                    datagrid.Append("<div class=\"datagrid-header-inner\" style=\"display: block;\">");
                    datagrid.Append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"datagrid-btable\" style=\"height: 25px;\">");
                    datagrid.Append("<tbody>");

                    datagrid.Append("<tr class=\"datagrid-header-row\">");
                    datagrid.Append("<td field=\"col1\" class=\"\">");
                    datagrid.Append("<div class=\"datagrid-cell\" style=\"width: 150px;\">");
                    datagrid.Append("<span>Created Date</span>");
                    datagrid.Append("<span class=\"datagrid-sort-icon\">&nbsp;</span>");
                    datagrid.Append("</div>");
                    datagrid.Append("</td>");
                    datagrid.Append("<td field=\"col2\" class=\"\">");
                    datagrid.Append("<div class=\"datagrid-cell\" style=\"width: 200px;\">");
                    datagrid.Append("<span>Full Name</span>");
                    datagrid.Append("<span class=\"datagrid-sort-icon\">&nbsp;</span>");
                    datagrid.Append("</div>");
                    datagrid.Append("</td>");
                    datagrid.Append("<td field=\"col3\">");
                    datagrid.Append("<div class=\"datagrid-cell\" style=\"width: 100px;\">");
                    datagrid.Append("<span>Veda file No.</span>");
                    datagrid.Append("<span class=\"datagrid-sort-icon\">&nbsp;</span>");
                    datagrid.Append("</div>");
                    datagrid.Append("</td>");
                    datagrid.Append("<td field=\"col4\">");
                    datagrid.Append("<div class=\"datagrid-cell\" style=\"width: 150px;\">");
                    datagrid.Append("<span>Record Age</span>");
                    datagrid.Append("<span class=\"datagrid-sort-icon\">&nbsp;</span>");
                    datagrid.Append("</div>");
                    datagrid.Append("</td>");
                    datagrid.Append("<td field=\"col5\">");
                    datagrid.Append("<div class=\"datagrid-cell\" style=\"width: 150px;\">");
                    datagrid.Append("<span>Create by</span>");
                    datagrid.Append("<span class=\"datagrid-sort-icon\">&nbsp;</span>");
                    datagrid.Append("</div>");
                    datagrid.Append("</td>");
                    datagrid.Append("</tr>");

                    datagrid.Append("</tbody>");
                    datagrid.Append("</table>");
                    datagrid.Append("</div>");
                    datagrid.Append("</div>");
                    datagrid.Append("<div class=\"datagrid-body\" style=\"width: 798px; height: 120px; overflow: auto;\">");
                    datagrid.Append("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"datagrid-btable\" style=\"table-layout: auto;\">");
                    datagrid.Append("<tbody>");
                   
                    foreach (DataRow dr in dtCreditCheckHistory.Rows)
                    {
                        datagrid.AppendFormat("<tr id=\"tr" + j  + "\" class=\"datagrid-row\" onmouseover=\"mouseOverColor('tr" + j + "')\" onmouseout=\"mouseOutColor('tr" + j + "')\" onclick=\"rowSelectedcolor('tr" + j + "')\" datagrid-row-index=\"{0}\" id=\"datagrid-row-r5-2-0\" ondblclick=\"window.open('/Pages/Application/VedaResponseData/{1}', '_blank')\">", j++, dr["XMLFile"].ToString());
                        datagrid.Append("<td field=\"col1\">");
                        datagrid.AppendFormat("<div class=\"datagrid-cell datagrid-cell-c5-col1\" style=\";height:auto;width:150px;\">{0}</div>", dr["CreatedDate1"].ToString());
                        datagrid.Append("</td>");
                        datagrid.Append("<td field=\"col2\">");
                        datagrid.AppendFormat("<div class=\"datagrid-cell datagrid-cell-c5-col2\" style=\";height:auto;width:200px;\">{0}</div>", dr["FullName"].ToString());
                        datagrid.Append("</td>");
                        datagrid.Append("<td field=\"col3\">");
                        datagrid.AppendFormat("<div class=\"datagrid-cell datagrid-cell-c5-col3\" style=\";height:auto;width:100px;\">{0}</div>", dr["VedafileNO"].ToString());
                        datagrid.Append("</td>");
                        datagrid.Append("<td field=\"col4\">");
                        datagrid.AppendFormat("<div class=\"datagrid-cell datagrid-cell-c5-col4\" style=\";height:auto;width:150px;\">{0}</div>", dateCalculation(dr["CreatedDate"].ToString()));
                        datagrid.Append("</td>");
                        datagrid.Append("<td field=\"col5\">");
                        datagrid.AppendFormat("<div class=\"datagrid-cell datagrid-cell-c5-col5\" style=\";height:auto;width:150px;\">{0}</div>", dr["CreatedBy"].ToString());
                        datagrid.Append("</td>");
                        datagrid.Append("</tr>");
                    }
                    datagrid.Append("</tbody>");
                    datagrid.Append("</table>");
                    datagrid.Append("</div>");
                    datagrid.Append("<div class=\"datagrid-footer\" style=\"width: 798px;\">");
                    datagrid.Append("<div class=\"datagrid-footer-inner\" style=\"display: none;\"></div>");
                    datagrid.Append("</div>");
                    datagrid.Append("</div>");
                    datagrid.Append("</div>");
                    datagrid.Append("</div>");
                    datagrid.Append("</div>");
                }
                context.Response.Write(datagrid);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        private string dateCalculation(string date)
        {
            DateTime dob = Convert.ToDateTime(date);
            DateTime PresentYear = DateTime.Now;
            TimeSpan ts = PresentYear - dob;
            DateTime Age = DateTime.MinValue.AddDays(ts.Days);
            string returndate = string.Empty;
            if (Age.Year - 1 > 0)
                returndate = string.Format("{0} Yrs ", Age.Year - 1);
            if (Age.Month - 1 > 0)
                returndate += string.Format("{0} Mths ", Age.Month - 1);
            if (Age.Day - 1 > 0)
                returndate += string.Format("{0} Days", Age.Day - 1);
            if (returndate == string.Empty)
                returndate = "0 Days";
            return returndate;
        }
    }
}